using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WebBrowser
{
    public partial class BookmarksWindow : Window
    {
        private BookmarkManager _bookmarkManager;
        public event EventHandler<string>? BookmarkSelected;

        public BookmarksWindow(BookmarkManager bookmarkManager)
        {
            InitializeComponent();
            _bookmarkManager = bookmarkManager;
            LoadBookmarks();
        }

        private void LoadBookmarks()
        {
            var bookmarks = _bookmarkManager.GetBookmarks();
            BookmarksList.ItemsSource = bookmarks;
            
            // Show empty state if no bookmarks
            EmptyState.Visibility = bookmarks.Count == 0 ? Visibility.Visible : Visibility.Collapsed;
            BookmarksList.Visibility = bookmarks.Count == 0 ? Visibility.Collapsed : Visibility.Visible;
        }

        private void BookmarksList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (BookmarksList.SelectedItem is Bookmark bookmark)
            {
                BookmarkSelected?.Invoke(this, bookmark.Url);
                Close();
            }
        }

        private void DeleteBookmark_Click(object sender, RoutedEventArgs e)
        {
            if (sender is Button button && button.Tag is Bookmark bookmark)
            {
                var result = MessageBox.Show($"Are you sure you want to delete the bookmark '{bookmark.Title}'?", 
                                           "Delete Bookmark", MessageBoxButton.YesNo, MessageBoxImage.Question);
                
                if (result == MessageBoxResult.Yes)
                {
                    _bookmarkManager.RemoveBookmark(bookmark.Url);
                    LoadBookmarks();
                }
            }
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}